<?php
			/***************************************************************
			*  Copyright notice
			*
			*  (c) 2010 Grzegrz Bańka <grzegorz@grzegorzbanka.com>
			*  All rights reserved
			*
			*  This script is part of the TYPO3 project. The TYPO3 project is
			*  free software; you can redistribute it and/or modify
			*  it under the terms of the GNU General Public License as published by
			*  the Free Software Foundation; either version 2 of the License, or
			*  (at your option) any later version.
			*
			*  The GNU General Public License can be found at
			*  http://www.gnu.org/copyleft/gpl.html.
			*
			*  This script is distributed in the hope that it will be useful,
			*  but WITHOUT ANY WARRANTY; without even the implied warranty of
			*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
			*  GNU General Public License for more details.
			*
			*  This copyright notice MUST APPEAR in all copies of the script!
			***************************************************************/
			/**
 			 * [CLASS/FUNCTION INDEX of SCRIPT]
 			 *
 			 * Hint: use extdeveval to insert/update function index above.
 			 */

require_once(PATH_tslib.'class.tslib_pibase.php');


/**
			 * Plugin 'Moderacja Newsów' for the 'gb_ttnews_moderated' extension.
			 *
			 * @author	Grzegrz Bańka <grzegorz@grzegorzbanka.com>
			 * @package	TYPO3
			 * @subpackage	tx_gbttnewsmoderated
			 */
			class tx_gbttnewsmoderated_pi1 extends tslib_pibase {
				var $prefixId      = 'tx_gbttnewsmoderated_pi1';		// Same as class name
				var $scriptRelPath = 'pi1/class.tx_gbttnewsmoderated_pi1.php';	// Path to this script relative to the extension dir.
				var $extKey        = 'gb_ttnews_moderated';	// The extension key.
				var $pi_checkCHash = true;
	
						/**
						 * The main method of the PlugIn
						 *
						 * @param	string		$content: The PlugIn content
						 * @param	array		$conf: The PlugIn configuration
						 * @return	The content that is displayed on the website
						 */
	function main($content, $conf)	{
		$this->conf=$conf;
		$this->pi_loadLL();
		
		if($GLOBALS['BE_USER']->user['uid']!='' || $this->conf['show_is_fe']==1)
		{
			$content=$this->get_toolbar();
		}
		return $content;
	}
	function setmode($mode,$news)
	{
		if($mode==4)
		{
			$news_query=array('deleted'=>'1','tstamp'=>time());
			$query = $GLOBALS['TYPO3_DB']->UPDATEquery('tt_news', 'uid="'.$news.'"', $news_query);		
			$res = $GLOBALS['TYPO3_DB']->sql(TYPO3_db, $query);		
			
			$array_param = array(
			'useCacheHash' =>1,
			'parameter' => $this->conf['pid_deleted'].' _self',
			'returnLast'=>'url'
			);			
			header('Location: http://'.$_SERVER['SERVER_NAME'].'/'.$this->cObj->typolink('',$array_param));
			exit;
		}
		
		if($this->conf['ismoderated_category']==1 && intval($this->conf['moderated_category'])!=0)
		{				
			
			$query = $GLOBALS['TYPO3_DB']->DELETEquery('tt_news_cat_mm', 'uid_local="'.$news.'" AND uid_foreign="'.$this->conf['moderated_category'].'"');		
			$res = $GLOBALS['TYPO3_DB']->sql(TYPO3_db, $query);	
		}
		
		$cat_special_uid=explode(',',$this->conf['cat_special_uid']);
		$cat_special_pid=explode(',',$this->conf['cat_special_pid']);
		
		$catpid=array($this->conf['cat_special']);
		$beuser=$GLOBALS['BE_USER']->user['uid'];

		$where = 'deleted=0 AND hidden=0 AND uid='.$news;
		$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('pid,tx_mtfenews_user,datetime,author_email','tt_news',$where);		
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
		{
			$user=$row['tx_mtfenews_user'];
			$tx_mtprize_mtprize=$row['tx_mtprize_mtprize'];
			$pid=$row['pid'];
			$author_email=$row['author_email'];
		}
		
		$where = 'uid='.$user;
		$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('tx_mtprize_mtprize,email,username','fe_users',$where);		
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
		{
			$tx_mtprize_mtprize=$row['tx_mtprize_mtprize'];
			$fe_email=$row['email'];
			$username =$row['username'];
		}

			$log=array(
				'oldRecord' => array('datetime'=>$row['datetime']),
				'newRecord' => array('datetime'=>time())
			);
			$sys_query=array(
				'userid'	=>	$beuser,
				'action'	=>	2,
				'recuid'	=>	$news,
				'tablename'	=>	'tt_news',
				'details'	=>	"Record (".$news.") został zmoderowany. MOD($mode) Użytkownik otrzymał punktów(".$this->conf['gold_'.$mode].").",
				'tstamp'	=>	time(),
				'type'		=>	1,
				'details_nr'	=>	10,
				'IP'			=>	$_SERVER['REMOTE_ADDR'],
				'log_data'		=>	serialize($log),
				'event_pid'	=>	$pid			
			);
			$GLOBALS['TYPO3_DB']->exec_INSERTquery(
					'sys_log ',
					$sys_query
			);
			
			$where = 'uid_local='.$news;
			$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid_foreign','tt_news_cat_mm',$where,'','sorting DESC','1');		
			while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
			{	
				$cat=$row['uid_foreign'];
			}
			if($this->conf['pid_from_cat_shortcut']==1)
			{			
				$where = 'uid='.$cat;
				$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('shortcut,parent_category','tt_news_cat',$where);		
				while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
				{	
					$shortcut=$row['shortcut'];
					$parent_category=$row['parent_category'];
				}
				if($shortcut=='' || $shortcut==0)
				{
					if($parent_category!=0) $shortcut=$this->get_cat_shortcut_fromparent($parent_category);
				}
				$pid=$shortcut;
			}
			elseif($this->conf['pid_from_cat_mainfolder']==1)
			{
				//tx_mtfenews_page_pid
				$where = 'uid='.$cat;
				$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('tx_mtfenews_page_pid,parent_category','tt_news_cat',$where);		
				while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
				{	
					
					$shortcut=$row['tx_mtfenews_page_pid'];
					$parent_category=$row['parent_category'];
				}
				
				if($shortcut=='' || $shortcut==0)
				{
					if($parent_category!=0) $shortcut=$this->get_cat_mainfolder_fromparent($parent_category);
				}
				$pid=$shortcut;

			}
			else
			{
					
				if(in_array($pid,$catpid))
				{
					for($i=0; $i<count($cat_special_uid); $i++)
					{
						if($cat==$cat_special_uid[$i])
						{
							$pid=$cat_special_pid[$i];
							break;
						}
					}
				}else
				{
					$cat_pid_old=explode(',',$this->conf['cat_pid_old']);
					$cat_pid_new=explode(',',$this->conf['cat_pid_new']);
	
					for($i=0; $i<count($cat_pid_old); $i++)
					{
						if($pid==$cat_pid_old[$i])
						{
							$pid=$cat_pid_new[$i];
							break;
						}
					}	
		
				}
			}
			

			$news_query=array('deleted'=>'1','tstamp'=>time());
			$query = $GLOBALS['TYPO3_DB']->UPDATEquery('tx_skpagecomments_comments', 'pivar="tx_ttnews[tt_news]='.$news.'"', $news_query);		
			$res = $GLOBALS['TYPO3_DB']->sql(TYPO3_db, $query);	

			$news_query=array('pid'=>$pid,'tstamp'=>time());
			//print_r($news_query);exit;
			$query = $GLOBALS['TYPO3_DB']->UPDATEquery('tt_news', 'uid="'.$news.'"', $news_query);		
			$res = $GLOBALS['TYPO3_DB']->sql(TYPO3_db, $query);	
			list($alink,$link)=$this->get_newslink($news,$cat);
			$user_query=array(
				'pid'		=>	$this->conf['prize_conf'],
				'tstamp'	=>	time(),
				'crdate'	=>	time(),
				'deleted'	=>	0,
				'hidden'	=>	0,
				'point'		=>	$this->conf['gold_'.$mode],
				'fe_user'	=>	$user,
				'description'	=>	$this->conf['info_'.$mode].$alink,
				'add_point'		=>	1,
				'what_columnt'	=> 'tt_news',
				'what_uid'		=>	$news
			);
			$GLOBALS['TYPO3_DB']->exec_INSERTquery(
					'tx_mtprize_history ',
					$user_query
			);
			
			$tx_mtprize_mtprize=$tx_mtprize_mtprize+$this->conf['gold_'.$mode];
			$user_query=array('tx_mtprize_mtprize'=>$tx_mtprize_mtprize);
			$query = $GLOBALS['TYPO3_DB']->UPDATEquery('fe_users', 'uid="'.$user.'"', $user_query);		
			$res = $GLOBALS['TYPO3_DB']->sql(TYPO3_db, $query);	
				
				//print_r($_SERVER);exit;
					
			if($this->conf['info_isadd']==1)
			{
				if($author_email=='') $author_email=$fe_email;
				
				$send_name=$this->conf['send_name'];
				$send_email=$this->conf['send_email'];	
				
				$subject=$this->pi_getLL('info_isadd_text_title').' '.$this->conf['send_name'];			
				$body=str_replace('%adres%','<a href="http://'.$_SERVER['SERVER_NAME'].'/'.$link.'">link do wpisu</a>',$this->pi_getLL('info_isadd_text_desc'));
								
				$mail = t3lib_div::makeInstance('t3lib_mail_Message');
				
				$mail->setBody($bodyHtml, 'text/html');
				$mail->addPart($bodyText, 'text/plain');
				
				$mail->setFrom(array($send_email => $send_name));
				$mail->setTo(array($author_email => $username));
				
				$mail->setSubject($subject);
				$mail->setBody($body);
				$ss=$mail->send();							
			}
						
			header('Location: http://'.$_SERVER['SERVER_NAME'].'/'.$link);
			exit;
		
	}
	function get_cat_mainfolder_fromparent($cat)
	{
		$where = 'uid='.$cat;
		$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('tx_mtfenews_page_pid,parent_category','tt_news_cat',$where);		
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
		{	
			$shortcut=$row['tx_mtfenews_page_pid'];
			$parent_category=$row['parent_category'];

		}
		if($shortcut=='' || $shortcut==0)
		{
			if($parent_category!=0) $shortcut=$this->get_cat_mainfolder_fromparent($parent_category);
		}
		return $shortcut;
	}
	function get_cat_shortcut_fromparent($cat)
	{
		$where = 'uid='.$cat;
		$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('shortcut,parent_category','tt_news_cat',$where);		
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
		{	
			$shortcut=$row['shortcut'];
			$parent_category=$row['parent_category'];
		}
		if($shortcut=='' || $shortcut==0)
		{
			if($parent_category!=0) $shortcut=$this->get_shortcut_fromparent($parent_category);
		}
		return $shortcut;
	}
	function get_newslink($news,$cat)
	{
			$where = 'uid='.$cat;
			$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('single_pid','tt_news_cat',$where);		
			while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
			{	
				$single_pid=$row['single_pid'];
			}	
			if($single_pid==0) $single_pid=$this->conf['single_view'];

			$array_param = array(
			'useCacheHash' =>1,
			'parameter' => $single_pid.' _self',
			'additionalParams' => '&tx_ttnews[tt_news]='.$news,
			'returnLast'=>'url'
			);
			return array(' <a href="'.$this->cObj->typolink('',$array_param).'">zobacz news</a>',$this->cObj->typolink('',$array_param));

			//header('Location: '.$this->cObj->typolink('',$array_param));
	}
	function get_toolbar()
	{
		if(isset($this->piVars['setmode']))
		{
			$this->setmode(intval($this->piVars['setmode']),intval($_GET['tx_ttnews']['tt_news']));
		}
		if(isset($_GET['tx_ttnews']['tt_news']))
		{
			$user=$this->get_user(intval($_GET['tx_ttnews']['tt_news']));
			$navi=$this->get_navi();
		}
		return '<div id="modnavi">'.$navi.$user.'</div>';
	}
	function get_navi()
	{
		return '
		<div class="navimod">
			<form action="'.$_SESSION['REQUEST_URI'].'" method="post">
				
				<input name="'.$this->prefixId.'[setmode]" type="radio" value="1" /> Uznaj treść (punkty '.$this->conf['gold_1'].')</a> |
				<input name="'.$this->prefixId.'[setmode]" type="radio" value="2" /> Uznaj treść (punkty '.$this->conf['gold_2'].')</a> |
				<input name="'.$this->prefixId.'[setmode]" type="radio" value="3" /> Uznaj treść (punkty '.$this->conf['gold_3'].')</a> |
				<input name="'.$this->prefixId.'[setmode]" type="radio" value="4" /> Nie uznawaj treści! (usuń treść)</a> 
				<input name="" type="submit" value="Moderuj" onClick="return confirm(\'Czy na pewno chcesz dokonać moderacji?\')" />
			</form>
		</div>
		';
	}
	function get_user($news)
	{
		$return='Taki użytkownik nie istnieje lub został usunięty / zbanowany';
		$where = 'deleted=0 AND hidden=0 AND uid='.intval($_GET['tx_ttnews']['tt_news']);
		$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('tx_mtfenews_user','tt_news',$where);
		
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
		{
			$user=$row['tx_mtfenews_user'];
		}
		$where = 'disable=0 AND uid='.$user;
		$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('username,name,email,crdate,lastlogin','fe_users',$where);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
		{
			$return='
			'.$row['username'].', '.$row['name'].', '.$row['email'].',
			'.date('d.m.y G:i',$row['crdate']).',
			'.date('d.m.y G:i',$row['lastlogin']).'
			';
		}
		return '<div class="userstat">'.$return.'</div>';
	}
}



if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/gb_ttnews_moderated/pi1/class.tx_gbttnewsmoderated_pi1.php'])	{
				include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/gb_ttnews_moderated/pi1/class.tx_gbttnewsmoderated_pi1.php']);
			}
			
			?>